<?php

namespace app\index\controller;
use think\Db;
use think\Controller;
class Ajax extends  Controller
{
    const STATUS_OK = 0;
    const STATUS_SYSTEM_ERROR = 10000;
    const STATUS_OPERATION_FAILED = 10001;
    const STATUS_RES_EMPTY = 10002;
    const STATUS_PARAMS_ERROR = 20001;
    const STATUS_OPERATION_REPEATED = 20002;
    const STATUS_REQMETHOD_ERROR = 20003;
    const STATUS_NOT_LOGIN = 30001;
    const STATUS_USERNAME_ERROR = 30002;
    const STATUS_USERNAME_EXIST = 30005;
    const STATUS_USERPWD_ERROR = 30003;
    const STATUS_USERVERFYCODE_ERROR = 30004;
    const STATUS_PERMISSIONS_ERROR = 40001;
    const STATUS_REPEAT_REGISTER = 40002;
    const STATUS_EMAIL_EXIST = 50001;
    const STATUS_EMAIL_ILLEGAL = 50002;
    const STATUS_TELEPHONE_ILLEGAL = 60001;
    const STATUS_TELEPHONE_EXIST = 60002;
    const STATUS_TIME_FORMAT_ERROR  = 70001;
    const STATUS_BUY_NO_MONEY = 80001;
    const STATUS_BUY_GOODS_NOTENOUGH = 80002;
    const STATUS_MECHANISM_EXIST = 90001;

    protected $_statusMsg = array(
        self::STATUS_OK => '成功',
        self::STATUS_SYSTEM_ERROR => '服务器异常',
        self::STATUS_OPERATION_FAILED => '失败',
        self::STATUS_RES_EMPTY => '结果为空',
        self::STATUS_PARAMS_ERROR => '参数异常',
        self::STATUS_OPERATION_REPEATED => '重复',
        self::STATUS_REQMETHOD_ERROR => '请求方法错误',
        self::STATUS_NOT_LOGIN => '用户未登录',
        self::STATUS_USERNAME_ERROR => '用户名错误',
        self::STATUS_USERNAME_EXIST => '用户名已存在',
        self::STATUS_USERPWD_ERROR => '密码错误',
        self::STATUS_USERVERFYCODE_ERROR => '验证码错误',
        self::STATUS_PERMISSIONS_ERROR => '没有该权限',
        self::STATUS_REPEAT_REGISTER => '重复注册',
        self::STATUS_EMAIL_EXIST => '邮箱已存在',
        self::STATUS_EMAIL_ILLEGAL => '邮箱格式不正确',
        self::STATUS_TELEPHONE_ILLEGAL => '手机不正确',
        self::STATUS_TELEPHONE_EXIST => '手机已存在',
        self::STATUS_TIME_FORMAT_ERROR => '时间格式错误',
        self::STATUS_BUY_NO_MONEY => '余额不足',
        self::STATUS_BUY_GOODS_NOTENOUGH => '名额不够',
        self::STATUS_MECHANISM_EXIST => '机构已存在',
    );

    protected $_data = array();
    public $_user = array();


    protected function initialize()
    {
        $this->_data['status']= self::STATUS_OK;
        $access_token = getParam('access_token', "");
        if ($access_token != "") {
            $cur_user_id = D('UserToken')->getUserIdByAccessToken($access_token);
            if ($cur_user_id > 0) {
                $this->_user = D('User')->getSelfInfo($cur_user_id);
            }
        }
    }

    public function echoJson()
    {
        $this->_data['errno'] = $this->_data['status'];
        if (!isset($this->_data['errmsg'])) {
            $this->_data['errmsg'] = strval($this->_statusMsg[$this->_data['errno']]);
        }
        unset($this->_data['status']);
        

        $jsonp = getParam("jsonVar", "");
        if ($jsonp != "") {
            echo "var {$jsonp} = " . json_encode($this->_data);
        } else {
            echo json_encode($this->_data);
        }
    }
}
